package com.atguigu.java8.datetime;

import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

public class TestLocalDateTime {

    @Test
    public void test1() {
        // LocalDateTime、LocalDate、LocalTime常用方法
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println(now.getYear());
        System.out.println(now.getMonthValue());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getHour());
        System.out.println(now.getMinute());
        System.out.println(now.getSecond());

        LocalDateTime dateTime = LocalDateTime.of(2024, 3, 8, 10, 48, 0);
        System.out.println(dateTime);

        LocalDateTime dateTime1 = now.plusYears(2);
        System.out.println(dateTime1);

        LocalDateTime dateTime2 = now.minusMonths(2);
        System.out.println(dateTime2);
    }

    @Test
    public void test2() {
        // Instant：时间戳（以Unix元年：1970年1月1日00:00:00到某个时间之间的毫秒值）
        Instant now = Instant.now();
        // 默认获取UTC时区
        System.out.println(now);

        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        System.out.println(now.toEpochMilli());

        Instant instant = Instant.ofEpochSecond(1000);
        System.out.println(instant);
    }

    @Test
    public void test3() {
        // Duration：计算两个“时间”之间的间隔
        Instant now = Instant.now();
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1));
        Instant now1 = Instant.now();
        Duration duration = Duration.between(now, now1);
        System.out.println(duration);
        System.out.println(duration.toMillis());

        System.out.println("----------------------------------");

        LocalDateTime now2 = LocalDateTime.now();
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2));
        LocalDateTime now3 = LocalDateTime.now();
        Duration between = Duration.between(now2, now3);
        System.out.println(between.toMillis());
    }

    @Test
    public void test4() {
        // Period：计算两个“日期”之间的间隔
        LocalDate now = LocalDate.now();
        LocalDate localDate = LocalDate.of(2022, 10, 1);
        Period period = Period.between(localDate, now);
        System.out.println(period);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

}
